package com.x.gzcrm.web.security;

import com.x.gzcrm.validate.code.ValidateCodeProcessor;
import com.x.gzcrm.validate.code.image.ImageCode;
import com.x.gzcrm.validate.code.image.ImageCodeException;
import org.springframework.security.authentication.AuthenticationServiceException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.filter.GenericFilterBean;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@Component
public class VerifyCodeFilter extends GenericFilterBean {
    private String defaultFilterProcessUrl = "/login";
    private AuthenticationFailureHandler authenticationFailureHandler;
    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
            throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;
        if ("POST".equalsIgnoreCase(request.getMethod()) && defaultFilterProcessUrl.equals(request.getServletPath())) {
            // 验证码验证
            String requestCaptcha = request.getParameter("code");
            String genCaptcha = (String) request.getSession().getAttribute("index_code");
            System.out.println("前端"+requestCaptcha+"生成的验证码"+genCaptcha);
//            try {
//                // 验证验证码是否正确
//                validate(request);
//            } catch (ImageCodeException e) {
//                authenticationFailureHandler.onAuthenticationFailure(request, response, e);
//                return;
//            }
            if (StringUtils.isEmpty(requestCaptcha)) {
                throw new AuthenticationServiceException("验证码不能为空!");

            }
            if (!genCaptcha.toLowerCase().equals(requestCaptcha.toLowerCase())) {
                throw new AuthenticationServiceException("验证码错误!");
            }

        }
        chain.doFilter(request, response);
    }

//    private void validate(HttpServletRequest request) {
//        ImageCode imageCodeSession = (ImageCode)request.getSession().getAttribute(ValidateCodeProcessor.SESSION_KEY_PREFIX + "IMAGE");
//        String imageCodeRequest = request.getParameter("code");
//        System.out.println("imageCodeSession="+imageCodeSession+"imageCodeRequest="+imageCodeRequest);
//        if (imageCodeRequest == null || imageCodeRequest.isEmpty()) {
//            throw new ImageCodeException("图片验证码不能为空");
//        }
//        if (imageCodeSession == null) {
//            throw new ImageCodeException("验证码不存在");
//        }
//        if (imageCodeSession.isExpired()) {
//            request.getSession().removeAttribute(ValidateCodeProcessor.SESSION_KEY_PREFIX + "IMAGE");
//            throw new ImageCodeException("验证码已过期");
//        }
//        if(!imageCodeRequest.equalsIgnoreCase(imageCodeSession.getCode())) {
//            throw new ImageCodeException("验证码错误");
//        }
//        request.getSession().removeAttribute(ValidateCodeProcessor.SESSION_KEY_PREFIX + "IMAGE");
//    }

    public AuthenticationFailureHandler getAuthenticationFailureHandler() {
        return authenticationFailureHandler;
    }

    public void setAuthenticationFailureHandler(AuthenticationFailureHandler authenticationFailureHandler) {
        this.authenticationFailureHandler = authenticationFailureHandler;
    }

}